#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update: 2010/10/30 11:32:57

'''docstring
'''

__revision__ = '0.1'
__author__ = 'lxd'

import sys
sys.path.append('.')
sys.path.append('..')
import wx
import pickle
import tools
import basic, farm, build
import config
import wxHelper

class MainFrame(wx.Dialog):
    def __init__(self, log, dm, name=u'系统设置', data=None,**kwargs):
        wx.Dialog.__init__(self, None, -1, name, size=(600, 600))
        self.log = log
        self.dm = dm
        self.kwargs= kwargs

        self.nb = wx.Notebook(self, size = (600, 400))
        self.setPages()
        self.btnSave = wx.Button(self, wx.ID_OK, u'保存')
        self.Bind(wx.EVT_BUTTON, self.OnSaveClick, self.btnSave)  
        self.btnCanel = wx.Button(self, wx.ID_CANCEL, u'取消')
        self.Bind(wx.EVT_BUTTON, self.OnCanelClick, self.btnCanel)  
        self.Bind(wx.EVT_CLOSE, self.OnClose)  
        self.lblTip = wx.StaticText(self, -1, u'带(*)的只有开启高级功能后才可生效')
        self.layout()

        self.data = data
        self.setData()

    def layout(self):
        self.Center()
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer.Add(self.btnSave, 0, wx.ALL, 5)
        btnSizer.Add(self.btnCanel, 0, wx.ALL, 5)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(self.nb, 0, wx.EXPAND|wx.ALL, 5)
        mainSizer.Add(self.lblTip, 0, wx.EXPAND|wx.ALL, 5)
        mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.ALL, 5)

        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        
    def setPages(self):
        """改这里就可以添加新项
        """
        self.basicPage = basic.MainPanel(self.nb, self.log, **self.kwargs)
        self.farmPage = farm.MainPanel(self.nb, self.log, **self.kwargs)
        self.buildPage = build.MainPanel(self.nb, self.log, self.dm, **self.kwargs)
        self.nb.AddPage(self.basicPage, u'基本设置')
        self.nb.AddPage(self.farmPage, u'农场设置')
        self.nb.AddPage(self.buildPage, u'建筑设置')

        self.pages = ('basicPage', 'farmPage', 'buildPage')

    def OnSaveClick(self, evt):
        data = self.getData()
        data = pickle.dumps(data)
        tools.saveData(config.user_file, data)
        self.Destroy()

    def OnCanelClick(self, evt):
        self.Close()

    def OnClose(self, evt):
        data = self.getData()
        if self.data != data:
            result = wxHelper.confirm(u'设置已改动，要保存吗？')
            if result == wx.ID_YES:    
                self.OnSaveClick(evt)
            elif result == wx.ID_CANCEL: 
                return False
        self.Destroy()



    def getData(self):
        data = {}
        for page in self.pages:
            data = getattr(self, page).getData(data)
        return data

    def setData(self):
        if self.data:
            for page in self.pages:
                getattr(self, page).setData(self.data)
        
if __name__ == '__main__':
    import dmsoft
    app = wx.PySimpleApp()
    main = MainFrame(config.log, dmsoft.dm, data = config.data)
    main.Show()
    app.MainLoop()
